{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "1a3c8803",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['ford prefect', 'male', 'researcher', 'betelgeuse seven']\n",
      "['name', 'ford perfect', 'gender', 'male', 'occupation', 'researcher', 'home planet', 'betelgeuse seven']\n"
     ]
    }
   ],
   "source": [
    "# 什么时候不用列表\n",
    "# 举例1：方法1\n",
    "person1 = ['ford prefect','male','researcher','betelgeuse seven']\n",
    "print(person1)\n",
    "# 方法2：\n",
    "person2 = ['name','ford perfect','gender','male',\n",
    "           'occupation','researcher','home planet','betelgeuse seven']\n",
    "print(person2)\n",
    "# 由以上举例表明，当需要处理结构化数据时，应避免使用列表。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a3286fee",
   "metadata": {},
   "source": [
    "### 第三章 处理结构化数据-字典\n",
    "### 字典：用于保存键/值对集合，键部分通常为字符串，值部分通常为任意的一切对象\n",
    "### 字典使用{}大括号包围，每个键和值用引号包围，键和值可能是字符串，键和值之间用：号隔开\n",
    "### 字典是无序的，不会维持插入顺序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "21d1d765",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "betelgeuse seven\n",
      "ford prefect\n"
     ]
    }
   ],
   "source": [
    "person3 = {'name':'ford prefect',\n",
    "           'gender':'male',\n",
    "           'occupation':'researcher',\n",
    "           'home planet':'betelgeuse seven'}\n",
    "#               键           所关联的值对 对齐字典代码使之更易读\n",
    "# 用键来查找值 使用中括号\n",
    "print(person3['home planet'])\n",
    "print(person3['name'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "3202e6f7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'name': 'ford prefect', 'gender': 'male', 'occupation': 'researcher', 'home planet': 'betelgeuse seven', 'age': 33}\n"
     ]
    }
   ],
   "source": [
    "# 扩展字典 给新键赋予对象\n",
    "person3['age'] = 33\n",
    "print(person3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "5327c422",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'a': 0, 'e': 0, 'i': 0, 'o': 0, 'u': 0}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{'a': 0, 'e': 2, 'i': 0, 'o': 0, 'u': 0}"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 频度计数数据结构\n",
    "# 赋予变量found一个空字典\n",
    "found = {}\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "print(found)\n",
    "# 将元音关联值初始化为0，各个元音用作键\n",
    "found['e'] = found['e'] + 1\n",
    "# 重复使用found[]过于繁琐\n",
    "found\n",
    "found['e'] +=1\n",
    "# 将与e键关联的值递增2次\n",
    "found\n",
    "# python 中没有++、--操作符，仅有+=、-=操作符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "3ed04c6e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s)\n",
      "e was found 2 time(s)\n",
      "i was found 0 time(s)\n",
      "o was found 0 time(s)\n",
      "u was found 0 time(s)\n"
     ]
    }
   ],
   "source": [
    "# 迭代处理键和值\n",
    "# K表示键，found[K]用于访问值\n",
    "for i in found:\n",
    "    print(i,'was found',found[i],'time(s)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72718586",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 知识点：\n",
    "# 可把字典作为行集。每一行包含2列，一列存储一个键，第二列存储一个值\n",
    "# 字典用大括号包围，键/值对之间用逗号分隔，键和相应的值用冒号分隔\n",
    "# 访问字典数据要使用中括号，将键放在中括号中可以访问与键关联的值\n",
    "# python 的for循环可用于迭代处理字典，迭代时键会赋给循环变量，用来访问数据值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "8822fae9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s)\n",
      "e was found 2 time(s)\n",
      "i was found 0 time(s)\n",
      "o was found 0 time(s)\n",
      "u was found 0 time(s)\n"
     ]
    }
   ],
   "source": [
    "# 使用sorted函数（内置方法）控制输出顺序\n",
    "for i in sorted(found):\n",
    "    print(i,'was found',found[i],'time(s)')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d2aa7b56",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a was found 0 time(s).\n",
      "e was found 2 time(s).\n",
      "i was found 0 time(s).\n",
      "o was found 0 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "# 使用两个循环变量i,v,\n",
    "# items 方法迭代处理字典，返回一个键/值对列表\n",
    "for i,v in sorted(found.items()):\n",
    "    print(i,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "1d5c4389",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:hello kitty\n",
      "a was found 0 time(s).\n",
      "e was found 1 time(s).\n",
      "i was found 1 time(s).\n",
      "o was found 1 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input('provide a word to search for vowels:')\n",
    "# 创建一个空字典\n",
    "found = {}\n",
    "# 将与键关联的值初始化为0 * 字典的键必须初始化\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found[letter] += 1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "886b8f3e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# in 操作符 检查是否存在某个键\n",
    "fruits = {}\n",
    "fruits['apples'] = 10\n",
    "fruits\n",
    "'apples'in fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "da0fca08",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 制定布尔值时，可以使用True或False,这是解释器提供的常量，\n",
    "#首字母必须大写，因为解释器把true和false看做是变量名。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "44f25419",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "if 'bananas'in fruits:\n",
    "    fruits['bananas'] += 1\n",
    "else:\n",
    "    fruits['bananas'] = 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "fbeaba9b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 1}"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 使用not in \n",
    "if 'pears'not in fruits:\n",
    "    fruits['pears'] = 0\n",
    "fruits['pears'] += 1\n",
    "fruits    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "d0546a85",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'apples': 10, 'bananas': 2, 'pears': 3, 'pear': 1}"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# setdefault方法 调用setdefault替换if/not in 语句\n",
    "# 使用setdefault可避免keyerror异常\n",
    "fruits.setdefault('pears',0)\n",
    "fruits['pears'] += 1\n",
    "fruits"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "dc88d408",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:hello kitty\n",
      "a was found 0 time(s).\n",
      "e was found 1 time(s).\n",
      "i was found 1 time(s).\n",
      "o was found 1 time(s).\n",
      "u was found 0 time(s).\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input('provide a word to search for vowels:')\n",
    "found = {}\n",
    "found['a'] = 0\n",
    "found['e'] = 0\n",
    "found['i'] = 0\n",
    "found['o'] = 0\n",
    "found['u'] = 0\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        found.setdefault(letter,0)\n",
    "        found[letter] += 1\n",
    "for k,v in sorted(found.items()):\n",
    "    print(k,'was found',v,'time(s).')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0ec1a467",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 知识点：\n",
    "# 字典是无序的，如果需要在输出中对字典排序，要使用sorted内置函数\n",
    "# items方法允许按行迭代一个字典，一次迭代中，items方法会向for循环返回下一个键和关联值\n",
    "# 若试图访问字典中不存在的键，会导致keyerror\n",
    "# in/not in 操作符\n",
    "# setdefault方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f9ccb6c1",
   "metadata": {},
   "source": [
    "### 集合"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "93d37cc6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 集合 以大括号开始和结束\n",
    "# 集合中不允许有重复，可用于快速进行查找\n",
    "vowels = {'a','e','e','i','o','u','u','o'}\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "e112cf30",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to search for vowels:hello kitty\n",
      "e\n",
      "o\n",
      "i\n"
     ]
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "word = input('provide a word to search for vowels:')\n",
    "found = []\n",
    "for letter in word:\n",
    "    if letter in vowels:\n",
    "        if letter not in found:\n",
    "            found.append(letter)\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "7bcb935a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = {'a','e','e','i','o','u','u'}\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "id": "0b6f038a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'i', 'o', 'u'}"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# set函数\n",
    "vowels2 = set('aeeiouu')\n",
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "id": "18bfcf9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'e', 'h', 'i', 'l', 'o', 'u'}"
      ]
     },
     "execution_count": 45,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = set('aeiou')\n",
    "word = 'hello'\n",
    "# union方法（并集） 将一个集合与另一集合合并，在将结果赋给新的变量u\n",
    "u = vowels.union(set(word))\n",
    "u"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "id": "2bb6cf75",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'h', 'i', 'l', 'o', 'u']"
      ]
     },
     "execution_count": 46,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#使用sorted有序排列集合中的元素\n",
    "u_list = sorted(list(u))\n",
    "u_list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "id": "13dd47ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'a', 'i', 'u'}"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# difference方法 显示集合与另一集合的差集\n",
    "d = vowels.difference(set(word))\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "61e2acb5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'e', 'o'}"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# intersection方法 （交集）\n",
    "i = vowels.intersection(set(word))\n",
    "i"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1c0ef77f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 知识点：\n",
    "# Python集合中不允许重复\n",
    "# 集合使用大括号包围，对象之间用逗号分隔\n",
    "# 集合不维持插入顺序\n",
    "#可向set 函数传递任何序列，由序列中的对象创建元素集合\n",
    "# 集合提供大量内置功能"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "83ede03d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to serch for vowels:hello kitty\n",
      "{'o', 'i', 'e'}\n"
     ]
    }
   ],
   "source": [
    "vowels = {'a','e','i','o','u'}\n",
    "word = input('provide a word to serch for vowels:')\n",
    "i = vowels.intersection(set(word))\n",
    "print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "id": "3316bff6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "provide a word to serch for vowels:hello kitty\n",
      "o\n",
      "i\n",
      "e\n"
     ]
    }
   ],
   "source": [
    "vowels =set('aeiou')\n",
    "word = input('provide a word to serch for vowels:')\n",
    "found = vowels.intersection(set(word))\n",
    "for vowel in found:\n",
    "    print(vowel)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92414d35",
   "metadata": {},
   "source": [
    "### 元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cb4f4f0",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 元组使用小括号包围\n",
    "# 可使用type内置函数确认所创建的对象类型\n",
    "# 只有一个对象的元组需用逗号提示解释器此为一个元组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "e74b8fa9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels = ['a','e','i','o','u']\n",
    "type(vowels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "id": "abfce6a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2 = ('a','e','i','o','u')\n",
    "type(vowels2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "id": "796f5c0a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'i', 'o', 'u']"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "id": "9bb067a5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('a', 'e', 'i', 'o', 'u')"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "id": "3546d1c7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['a', 'e', 'I', 'o', 'u']"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "vowels[2] = 'I'\n",
    "vowels"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "id": "63373f36",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tuple"
      ]
     },
     "execution_count": 61,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "b = ('python',)\n",
    "type(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2bd3dfe0",
   "metadata": {},
   "outputs": [],
   "source": [
    "## 本次学习至P136"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b89886e4",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cd11941",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0d186b9a",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ce5120c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
